%onsets_dir = '/autofs/space/clive_001/users/adriana/GE_SVM/GE_grand_average_results/biphone_second/20220418-1002/';
%codas_dir = '/autofs/space/clive_001/users/adriana/GE_SVM/GE_grand_average_results/biphone_first/20220418-1002/';

onsets_dir = '/autofs/space/clive_001/users/adriana/GE_SVM/GE_grand_average_results/final/20221125-1813/';
codas_dir = '/autofs/space/clive_001/users/adriana/GE_SVM/GE_grand_average_results/initial/20221125-1813/';

onsets_result_files = dir([onsets_dir, '*.mat']);
codas_result_files = dir([codas_dir, '*.mat']);

%ROIs = {'R-preCG_2', 'R-postCG_2', 'R-postCG_3', 'R-MTG_4', 'R-ITG_2', 'R-STG_2',...
%    'R-MTG_2', 'R-STG_3', 'R-MTG_1', 'R-ITG_1', 'R-MTG_3', 'R-STG_1', 'L-postCG_1'...
%    'L-SMG_1', 'L-ParaHip_1', 'L-STG_2', 'L-ITG_2', 'L-MTG_1', 'L-STG_1', 'L-ITG_1', 'L-MTG_2'};
ROIs = {'','R-LOC_1', 'R-ParsOrb_1', 'R-SFG_2', 'R-SFG_3', 'R-SFG_1', 'R-preCG_2',...
    'R-preCG_1', 'R-postCG_2', 'R-postCG_3', 'R-postCG_4', 'R-postCG_1', 'R-postCG_5', 'R-AG_1',...
    'R-ITG_2', 'R-ITG_1', 'R-ITG_3', 'R-MTG_4', 'R-MTG_2', 'R-MTG_1', 'R-MTG_3', 'R-STG_2', 'R-STG_3', 'R-STG_1',...
    'L-CC_1', 'L-ParaHip_1', 'L-ParsOrb_1', 'L-ParsTri_1', 'L-cMFG_1', 'L-SFG_1', 'L-postCG_1', 'L-SMG_1',...
    'L-SPC_1', 'L-ITG_2', 'L-ITG_1', 'L-TPol_1', 'L-MTG_1', 'L-MTG_2', 'L-STG_2', 'L-STG_1'};

pubROIs = {'','R-LOC_1', 'R-ParsOrb_1', 'R-SFG_3', 'R-SFG_2', 'R-SFG_1', 'R-preCG_2',...
    'R-preCG_1', 'R-postCG_5', 'R-postCG_4', 'R-postCG_3', 'R-postCG_2', 'R-postCG_1', 'R-AG_1',...
    'R-ITG_3', 'R-ITG_2', 'R-ITG_1', 'R-MTG_4', 'R-MTG_3', 'R-MTG_2', 'R-MTG_1', 'R-STG_3', 'R-STG_2', 'R-STG_1',...
    'L-CC_1', 'L-ParaHip_1', 'L-ParsOrb_1', 'L-ParsTri_1', 'L-cMFG_1', 'L-SFG_1', 'L-postCG_1', 'L-SMG_1',...
    'L-SPC_1', 'L-ITG_2', 'L-ITG_1', 'L-TPol_1', 'L-MTG_2', 'L-MTG_1', 'L-STG_2', 'L-STG_1'};

time = -100:1000;
ndecoders = 0;
figure('Position', [986, 340, 558, 651]);
area([100, 400], [length(ROIs)+1; length(ROIs)+1], 'FaceColor', [210, 210, 210]./255, 'FaceAlpha', 1); hold on;
area([400, 600], [length(ROIs)+1; length(ROIs)+1], 'FaceColor', [181, 255, 210]./255, 'FaceAlpha', 1); hold on;

for kfile = 1:length(onsets_result_files)
    file = onsets_result_files(kfile);
    load(fullfile(onsets_dir,file.name));
    if ~isempty(stat_ave.clusters)
        fnameparts = split(file.name, {'_', '-'});
        roiname = sprintf('%s-%s_%s', fnameparts{3}, fnameparts{4}, fnameparts{5});
        ndecoders = find(ismember(ROIs, roiname));
        if ~isempty(ndecoders)
        for kcluster = 1:length(stat_ave.clusters)
            timestart = time(stat_ave.clusters{kcluster}(1));
            timeend = time(stat_ave.clusters{kcluster}(end));
            plot([timestart, timeend], [ndecoders+0.2, ndecoders+0.2], '|-', 'Color', [0,77,64]/255, 'LineWidth', 2.5); hold on;
        end
        end
    end
end


for kfile = 1:length(codas_result_files)
    file = codas_result_files(kfile);
    load(fullfile(codas_dir, file.name));
    if ~isempty(stat_ave.clusters)
        fnameparts = split(file.name, {'_', '-'});
        roiname = sprintf('%s-%s_%s', fnameparts{3}, fnameparts{4}, fnameparts{5});
        ndecoders = find(ismember(ROIs, roiname));
        if ~isempty(ndecoders)
        for kcluster = 1:length(stat_ave.clusters)
            timestart = time(stat_ave.clusters{kcluster}(1));
            timeend = time(stat_ave.clusters{kcluster}(end));
            plot([timestart, timeend], [ndecoders-0.2, ndecoders-0.2], '|:', 'Color', [214, 161, 1]/255, 'LineWidth', 2.5); hold on;
        end
        end
    end
end

%set(gca, 'TickLabelInterpreter', 'none');
set(gca, 'YLim', [0, length(ROIs)+1], 'YTick', 2:length(ROIs), 'YTickLabels', pubROIs(2:end));
set(gca, 'XLim', [-100, 1000]);
plot([350, 350], [0, length(ROIs)+1], 'k--', 'LineWidth', 1);
text(350, 1, '<-pre-offset ', 'HorizontalAlignment', 'right');
text(350, 1, ' post-offset->', 'HorizontalAlignment', 'left');
xlabel('Time (ms)');

%%
late_nonsig = {'R-ITG_1'};
late_sig = {'R-preCG_2', 'R-ITG_3', 'R-STG_2', 'L-STG_1'};
early_nonsig = {'L-postCG_1'};
early_sig = {'R-ParsOrb_1', 'R-preCG_2', 'R-postCG_2', 'R-ITG_1', 'R-ITG_2', 'R-MTG_4', 'R-MTG_2', 'R-MTG_1', 'R-STG_2', 'R-STG_3', 'R-STG_1', 'L-MTG_1', 'L-STG_1'};
for k = 1:length(early_nonsig)
    text(250, find(ismember(ROIs, early_nonsig(k))), 'n.s.', 'HorizontalAlignment', 'center');
end

for k = 1:length(late_nonsig)
    text(500, find(ismember(ROIs, late_nonsig(k))), 'n.s.', 'HorizontalAlignment', 'center');
end

asteriskTime = [repmat(250, 1, length(early_sig)), repmat(500, 1, length(late_sig))];
asteriskROI = [find(ismember(ROIs, early_sig)), find(ismember(ROIs, late_sig))];
scatter(asteriskTime, asteriskROI, [], 'k', '*');
h1 = plot([1000, 2000], [100, 100], '-', 'Color', [0,77,64]/255, 'LineWidth', 2.5);
h2 = plot([1000, 2000], [100, 100], ':', 'Color', [214, 161, 1]/255, 'LineWidth', 2.5);
legend([h1, h2], {'Initial', 'Final'}, 'location', 'northeast');
